package cn.edu.zut.controller;

import cn.edu.zut.domin.User;
import cn.edu.zut.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 用户web
 */
@Controller
@RequestMapping("/user")
public class UserController {


    //加载配置文件
    private ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    //获取对象
    private IUserService iUserService = (IUserService) ac.getBean("userService");

//    private final IUserService iUserService;
//
//    @Autowired
//    public UserController(@Qualifier("userServiceImpl") IUserService iUserService) {
//        this.iUserService = iUserService;
//    }

    /**
     * 查询全部用户--未分页
     * @return ModelAndView
     * @throws Exception
     */
//    @RequestMapping("/findAll")
//    public ModelAndView findAll() throws Exception {
//
//        ModelAndView mv = new ModelAndView();
//
//        List<User> users = iUserService.findAll();
//        mv.addObject("userList", users);
//        mv.setViewName("user-list");
//        return mv;
//    }

    /**
     * 查询全部用户--分页
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll")
    //默认第一页,每页显示四个
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<User> users = iUserService.findAll(page, size);
        //分页Bean PageInfo就是一个分页的Bean
        PageInfo<User> pageInfo = new PageInfo<>(users);
        //总页数等....
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("user-list");
        return mv;
    }

    /**
     * 插入某个用户的信息
     *
     * @param user
     * @return String
     * @throws Exception
     */
    @RequestMapping("/save")
    public String saveUser(User user) throws Exception {
        iUserService.saveUser(user);
        //保存完成后跳转到findAll
        return "redirect:findAll";
    }


    //
//    ModelAndView mv = new ModelAndView();
//
//        List<User> users = iUserService.findAll();
//        mv.addObject("userList", users);
//        mv.setViewName("user-list");
//        return mv;


    /**
     * @param id
     * @param password
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    public ModelAndView login(String id, String password) throws Exception {
        User user=iUserService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("userList1",user);
        if(user==null) mv.setViewName("failer");
        else if(!user.getPassword().equals(password)) mv.setViewName("failer");
        else if(user.getStatus().equals("管理员")) mv.setViewName("main");
        else mv.setViewName("mainReaderPart");
        return mv;
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String deleteUserById(String id) throws Exception {
        iUserService.deleteUserById(id);
        //删除完成后回到全部信息页面
        return "redirect:findAll";
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping("/update")
    public String updateUser(User user) throws Exception {
        iUserService.updateUser(user);
        return "redirect:findAll";
    }

    /**
     * 按照姓名查询用户
     * @param username
     * @return
     * @throws Exception
     */
    @RequestMapping("/findUserByName")
    public ModelAndView findByUserName(String username) throws Exception {
        System.out.println(username);
        ModelAndView mv = new ModelAndView();
        List<User> users = iUserService.findByUserName(username);
        mv.addObject("userList", users);
        mv.setViewName("user-list");
        return mv;
    }
}